package imvu.rbox.object
{
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.system.fscommand;
	import flash.utils.Timer;
	
	import imvu.rbox.event.ActionEvent;
	import imvu.rbox.global.GStatic;
	import imvu.rbox.struct.BoxInfo;

	public class ActionObject extends Sprite
	{
		public var mcBoxPlace1:Sprite;		//	元件方块放置位置1
		public var mcBoxPlace2:Sprite;		//	元件方块放置位置2
		public var btnChange:SimpleButton;	//	变形按钮
		public var btnLeft:SimpleButton;	//	左移按钮
		public var btnRight:SimpleButton;	//	右移按钮
		public var btnDown:SimpleButton;	//	下移按钮
		public var btnFastDown:SimpleButton;	//	快速下落
		
		private var objArray:Array;		//	方块对象数组
		private var nextArray:Array;	//	下一个方块数组
		private var nowInfo:BoxInfo;	//	当前方块的状信息
		private var nextInfo:BoxInfo;	//	下一个要出现的方块的状态信息
		
		private var gTime:Timer;		//	用于延时发送消息的对象
				
		public function ActionObject()
		{
			objArray = new Array();
			nextArray = new Array();
			
			for(var i:int = 0; i < 4; i++)
			{
				objArray[i] = new Array();
				nextArray[i] = new Array();
				for(var j:int = 0; j < 4; j++)
				{
					var obj:BoxMovie = new BoxMovie();
					obj.x = j * obj.width;
					obj.y = i * obj.height;
					this.mcBoxPlace1.addChild(obj);
					
					var nObj:BoxMovie = new BoxMovie();
					nObj.x = j * nObj.width;
					nObj.y = i * nObj.height;
					nObj.alpha = 0.35;
					this.mcBoxPlace2.addChild(nObj);
					
					objArray[i][j] = obj;
					nextArray[i][j] = nObj;
				}
			}
			
			this.btnChange.addEventListener(MouseEvent.CLICK, onClickButtons);
			this.btnFastDown.addEventListener(MouseEvent.CLICK, onClickButtons);
			
			SetButtonListener(this.btnLeft);
			SetButtonListener(this.btnRight);
			SetButtonListener(this.btnDown);
			
			this.addEventListener(Event.ADDED_TO_STAGE, onSetStageListener);
		}
		
		private function onSetStageListener(e:Event):void
		{
			flash.system.fscommand("trapallkeys", "true");
			
			this.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownEvent);			
		}
		
		public function InitAction():void
		{
			StopKeyListener();
			
			this.box = new BoxInfo();
			this.box = new BoxInfo();
		}
		
		//	设置当前要显示出来的对象
		public function set box(bInfo:BoxInfo):void
		{
			var tArray:Array;
			var nArray:Array;
			var i:int, j:int;				
			
			if(this.nextInfo)
			{
				// trace("Rush objArray");
				
				this.nowInfo = new BoxInfo();
				this.nowInfo.id = this.nextInfo.id;
				this.nowInfo.arrow = this.nextInfo.arrow;
		
				nArray = this.nowInfo.toArray();
						
				for(i = 0; i < objArray.length; i++)
				{
					for(j = 0; j < objArray[i].length; j++)
					{
						if(nArray)
						{
							(objArray[i][j] as BoxMovie).id = nArray[i][j];
						}
						else
						{
							(objArray[i][j] as BoxMovie).id = 0;
						}
					}
				}
			}
			
			this.nextInfo = bInfo;
			
			if(bInfo)
			{
				tArray = bInfo.toArray();
			}
			
			// trace("rusn nextArray");
			// trace("要显示的方块数组是：" + tArray.join("\n"));
			
			for(i = 0; i < nextArray.length; i++)
			{
				for(j = 0; j < nextArray[i].length; j++)
				{
					if(tArray)
					{
						(nextArray[i][j] as BoxMovie).id = tArray[i][j];
					}
					else
					{
						(nextArray[i][j] as BoxMovie).id = 0;
					}
				}
			}
		}
		
		//	取得当前的对象
		public function get box():BoxInfo
		{
			return nowInfo;
		}		
		
		private function set nextBox(bInfo:BoxInfo):void
		{
			var tArray:Array = new Array();
			this.nextInfo = bInfo;
			
			if(bInfo)
			{
				tArray = bInfo.toArray();
			}
			
			// trace("要显示的方块数组是：" + tArray.join("\n"));
			
			for(var i:int = 0; i < objArray.length; i++)
			{
				for(var j:int = 0; j < objArray[i].length; j++)
				{
					if(tArray)
					{
						(objArray[i][j] as BoxMovie).id = tArray[i][j];
					}
					else
					{
						(objArray[i][j] as BoxMovie).id = 0;
					}
				}
			}
		}
		//-------------------------------------------------------------------
		
		private var nowClick:SimpleButton;		//	当前点击的对象
		private var waitTime:Timer;				//	等待时间
		private var isMove:Boolean;				//	是否自动移动过
		
		private function SetButtonListener(btn:SimpleButton):void
		{
			btn.addEventListener(MouseEvent.MOUSE_DOWN, onBtnDownEvent);
			btn.addEventListener(MouseEvent.MOUSE_UP, onBtnUpEvent);			
						
			// this.btnLeft.addEventListener(MouseEvent.CLICK, onClickButtons);
			// this.btnRight.addEventListener(MouseEvent.CLICK, onClickButtons);
			// this.btnDown.addEventListener(MouseEvent.CLICK, onClickButtons);
		}
		
		private function onBtnDownEvent(e:MouseEvent):void
		{
			this.nowClick = e.target as SimpleButton;
			
			isMove = false;
			
			if(e.target == this.btnDown)
			{
				StartKeyListener(50);
			}
			else
			{
				StartKeyListener(100);
			}	
			this.stage.addEventListener(MouseEvent.MOUSE_UP, onBtnUpEvent);		
		}
		
		private function onBtnUpEvent(e:MouseEvent):void
		{
			StopKeyListener();
			
			if(!isMove)
			{
				onSendEvent(null);
			}
						
			nowClick = null;
			this.stage.removeEventListener(MouseEvent.MOUSE_UP, onBtnUpEvent);
		}
		
		private function onKeyDownEvent(e:KeyboardEvent):void
		{
			switch(e.keyCode)
			{
				//	W 键(变形) J K 都属于变形 方向键向上键
				case 74 :
				case 75 :
			 	case 87 : 
			 	case 38 :
			 		this.dispatchEvent(new ActionEvent(ActionEvent.CHANGE_BOX));
			 		break;
			 	
			 	//	A 键(左移)
			 	case 65 :
			 	case 37 :
			 		this.dispatchEvent(new ActionEvent(ActionEvent.LEFT_MOVE));
			 		break;
			 	
			 	//	D 键(右移)
			 	case 68 :
			 	case 39 :
			 		this.dispatchEvent(new ActionEvent(ActionEvent.RIGHT_MOVE));
			 		break;
			 	
			 	//	S 键(下移)
			 	case 83 :
			 	case 40 :
			 		this.dispatchEvent(new ActionEvent(ActionEvent.DOWN_MOVE));
			 		break;
			 	
			 	//	空格键(快速下移)
			 	case 32 :
			 		this.dispatchEvent(new ActionEvent(ActionEvent.FASET_DOWN));
			 		break;
			}			
		}			
		
		private function StartKeyListener(wTime:Number):void
		{
			StopKeyListener();
			
			waitTime = new Timer(wTime);
			waitTime.addEventListener(TimerEvent.TIMER, onSendEvent);
			waitTime.start();
		}
		
		public  function StopKeyListener():void
		{
			if(waitTime)
			{
				waitTime.stop();
				waitTime.removeEventListener(TimerEvent.TIMER, onSendEvent);
				waitTime = null;
			}
		}
		
		private function onSendEvent(e:TimerEvent):void
		{
			isMove = true;
			
			switch(this.nowClick)
			{
				case this.btnLeft :
					this.dispatchEvent(new ActionEvent(ActionEvent.LEFT_MOVE));
					break;
				
				case this.btnRight :
					this.dispatchEvent(new ActionEvent(ActionEvent.RIGHT_MOVE));
					break;
				
				case this.btnDown :				
					this.dispatchEvent(new ActionEvent(ActionEvent.DOWN_MOVE));
					break;
			}
		}
		
		private function onClickButtons(e:MouseEvent):void
		{
			if(GStatic.isGameStart)
			{
				switch(e.target)
				{
					case this.btnChange :
						// this.nowInfo.boxData.id++;
						// this.box = this.nowInfo;
						this.dispatchEvent(new ActionEvent(ActionEvent.CHANGE_BOX));
						break;
					
					case this.btnLeft :
						// trace("抛出 左移 事件");
						this.dispatchEvent(new ActionEvent(ActionEvent.LEFT_MOVE));
						break;
					
					case this.btnRight :
						// trace("抛出 右移 事件");
						this.dispatchEvent(new ActionEvent(ActionEvent.RIGHT_MOVE));
						break;
					
					case this.btnDown :				
						// var boxObj:BoxInfo = new BoxInfo();
						// boxObj.id = Math.floor(Math.random() * 14) + 1;
						// boxObj.arrow = Math.floor(Math.random() * 4);					
						// this.box = boxObj;					
						// trace("getData::" + boxObj.boxData);
						this.dispatchEvent(new ActionEvent(ActionEvent.DOWN_MOVE));
						break;
					
					case this.btnFastDown :
						this.dispatchEvent(new ActionEvent(ActionEvent.FASET_DOWN));
						break;
				}
			}
		}				
	}
}